package org.fearsomej.list.client.ui;

import org.fearsomej.list.client.service.TaskService;
import org.fearsomej.list.client.service.TaskServiceAsync;
import org.fearsomej.list.dto.TaskDTO;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class TaskEntryForm extends VerticalPanel {
	private final TextBox title = new TextBox();
	private final Button save = new Button("Save Task");
	private final Label messageLabel = new Label();
	
	public TaskEntryForm() {
		this.add(new HTML("Add a Task"));
		this.add(messageLabel);
		this.add(title);
		this.add(save);
		save.addClickHandler(new ClickHandler() {
			
			@Override
			public void onClick(ClickEvent event) {
				final TaskServiceAsync taskService = (TaskServiceAsync) GWT
				.create(TaskService.class);
				TaskDTO task = new TaskDTO();
				task.setTitle(title.getText());
				task.setCalendarEntry(true);
				task.setSize("SMALL");
				taskService.createTask(task, new AsyncCallback<Void>() {

					@Override
					public void onSuccess(Void result) {
						title.setText("");
						messageLabel.setText("Task created successfully!");
					}

					@Override
					public void onFailure(Throwable caught) {
						messageLabel.setText("Error creating new task: "
								+ caught.toString());
				
					}

					
				});
				
			}
		});
	}
	
}
